package jsyf.oa.web.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.system.LeaveFacade;
import jsyf.oa.facade.system.SignInFacade;

/**
 * Created by WHH-PC on 2018/1/8.
 */
@Api(value = "签到接口", description = "签到接口")
@Controller
@RequestMapping("/signIn")
public class SignInController {
    @Resource
    private SignInFacade signInFacade;
    @Resource
    private LeaveFacade leaveFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "签到信息录入",
            notes = "签到信息录入",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:\"201712162019459996F69A44574C3E89\",remark:\"aaa\",address:\"bbb\",longitude:111,latitude:222,id:\"\",signOutLongitude:\"333\",signOutLatitude:\"444\",signOutAddress:\"签退地址\"}", dataType = "String", paramType = "body")
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String id = signInFacade.save(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @RequestMapping(value = "/saveFirstOrLastVisitDiff.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "保存签到与第一次/最后一次/面访的差距",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:\"201712162019459996F69A44574C3E89\",signDate:\"2018-09-17\",signOutAddress:\"\"}", dataType = "String", paramType = "body")
    public void saveFirstOrLastVisitDiff(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject objResult = signInFacade.saveFirstOrLastVisitDiff(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @RequestMapping(value = "/revertIn.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "签到信息撤回",
            notes = "签到信息撤回",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{id:\"201712162019459996F69A44574C3E89\"}", dataType = "String", paramType = "body")
    public void revertIn(HttpServletResponse response, @RequestBody String jsonData) {
        String id = signInFacade.revertIn(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @RequestMapping(value = "/revert.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "签退信息撤回",
            notes = "签退信息撤回",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:\"201712162019459996F69A44574C3E89\",remark:\"aaa\",address:\"bbb\",longitude:111,latitude:222,id:\"\",signOutLongitude:\"333\",signOutLatitude:\"444\",signOutAddress:\"签退地址\"}", dataType = "String", paramType = "body")
    public void revert(HttpServletResponse response, @RequestBody String jsonData) {
        String id = signInFacade.revert(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }


    @RequestMapping(value = "/saveLeave.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "请假信息录入",
            notes = "请假信息录入",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:\"201712182045404A99D3141F0994413E\",remark:\"bbbb\",beginTime:\"2018-01-09\",endTime:\"2018-01-09\"}", dataType = "String", paramType = "body")
    public void saveLeave(HttpServletResponse response, @RequestBody String jsonData) {
        String id = leaveFacade.save(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @RequestMapping(value = "/getSignInByDay.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取某天签到信息",
            notes = "获取某天签到信息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:\"201712182045404A99D3141F0994413E\",signDate:\"2018-01-09\"}", dataType = "String", paramType = "body")
    public void getSignInByDay(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        String signDate = obj.getString("signDate");
        JSONObject jsonObject = signInFacade.getSignInByDay(userId, signDate);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(jsonObject));
    }

    @RequestMapping(value = "/getLeaveByDay.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取某天请假信息",
            notes = "获取某天请假信息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:\"201712182045404A99D3141F0994413E\",leaveDate:\"2018-01-09\"}", dataType = "String", paramType = "body")
    public void getLeaveByDay(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        String leaveDate = obj.getString("leaveDate");
        JSONObject jsonObject = leaveFacade.getLeaveByDay(userId, leaveDate);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(jsonObject));
    }


    @RequestMapping(value = "/delLeaveByDay.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "销假",
            notes = "销假",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:\"201712182045404A99D3141F0994413E\",leaveDate:\"2018-01-09\"}", dataType = "String", paramType = "body")
    public void delLeaveByDay(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        String leaveDate = obj.getString("leaveDate");
        String id = leaveFacade.delLeaveByDay(userId, leaveDate);
        JSONObject objResult = new JSONObject();
        objResult.put("affectRow", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }


}
